rm(list = ls())
source("code/_preamble.R")

#________________________________
##Load the dataset
load("data/weekly_attack_cd_excluded.RData")


#________________________________
##TABLE A8

#________________________________
#general attack
out_general <- att_gt(yname = "attacks_binary",
                      gname = "first_treat",
                      idname = "town_num",
                      tname = "week",
                      xformla = ~ 1,
                      data = elections_cd_excluded[elections_cd_excluded$to_keep == 1,],
                      est_method = "reg",
                      clustervars = "town_num"
)

es_simple_general <- aggte(out_general, type = "simple", clustervars = "town_num")

#________________________________
#rebels attack
out_rebels <- att_gt(yname = "rebels_binary",
                     gname = "first_treat",
                     idname = "town_num",
                     tname = "week",
                     xformla = ~ 1,
                     data = elections_cd_excluded[elections_cd_excluded$to_keep == 1,],
                     est_method = "reg",
                     clustervars = "town_num"
)

es_simple_rebels <- aggte(out_rebels, type = "simple", clustervars = "town_num")

#________________________________
#gov attack
out_gov <- att_gt(yname = "government_binary",
                  gname = "first_treat",
                  idname = "town_num",
                  tname = "week",
                  xformla = ~ 1,
                  data = elections_cd_excluded[elections_cd_excluded$to_keep == 1,],
                  est_method = "reg",
                  clustervars = "town_num"
)

es_simple_gov <- aggte(out_gov, type = "simple", clustervars = "town_num")

#________________________________
##To print
res_control <- c(round(es_simple_rebels$overall.att, 3), str_c("(", (round(es_simple_rebels$overall.se, 3)), ")"),
                 round(es_simple_gov$overall.att, 3), str_c("(", (round(es_simple_gov$overall.se, 3)), ")"),
                 round(es_simple_general$overall.att, 3), str_c("(", (round(es_simple_general$overall.se, 3)), ")"))


#________________________________
##SAMPLE OF MODEL 3
rm(list = setdiff(ls(), c("elections_cd_excluded", "res_control")))

#________________________________
#general attack
out_general <- att_gt(yname = "attacks_binary",
                      gname = "first_treat",
                      idname = "town_num",
                      tname = "week",
                      xformla = ~ 1,
                      data = elections_cd_excluded[elections_cd_excluded$to_keep_dur == 1,],
                      est_method = "reg",
                      clustervars = "town_num"
)

es_simple_general <- aggte(out_general, type = "simple", clustervars = "town_num")

#________________________________
##rebels attack
out_rebels <- att_gt(yname = "rebels_binary",
                     gname = "first_treat",
                     idname = "town_num",
                     tname = "week",
                     xformla = ~ 1,
                     data = elections_cd_excluded[elections_cd_excluded$to_keep_dur == 1,],
                     est_method = "reg",
                     clustervars = "town_num"
)

es_simple_rebels <- aggte(out_rebels, type = "simple", clustervars = "town_num")

#________________________________
##gov attack
out_gov <- att_gt(yname = "government_binary",
                  gname = "first_treat",
                  idname = "town_num",
                  tname = "week",
                  xformla = ~ 1,
                  data = elections_cd_excluded[elections_cd_excluded$to_keep_dur == 1,],
                  est_method = "reg",
                  clustervars = "town_num"
)

es_simple_gov <- aggte(out_gov, type = "simple", clustervars = "town_num")

#________________________________
##To print
res_dur <- c(round(es_simple_rebels$overall.att, 3), str_c("(", (round(es_simple_rebels$overall.se, 3)), ")"),
                 round(es_simple_gov$overall.att, 3), str_c("(", (round(es_simple_gov$overall.se, 3)), ")"),
                 round(es_simple_general$overall.att, 3), str_c("(", (round(es_simple_general$overall.se, 3)), ")"))


#________________________________
##SAMPLE OF MODEL 1
rm(list = setdiff(ls(), c("elections_cd_excluded", "res_control", "res_dur")))

#________________________________
##general attack
out_general <- att_gt(yname = "attacks_binary",
                      gname = "first_treat",
                      idname = "town_num",
                      tname = "week",
                      xformla = ~ 1,
                      data = elections_cd_excluded,
                      est_method = "reg",
                      clustervars = "town_num"
)

es_simple_general <- aggte(out_general, type = "simple", clustervars = "town_num")

#________________________________
##rebels attack
out_rebels <- att_gt(yname = "rebels_binary",
                     gname = "first_treat",
                     idname = "town_num",
                     tname = "week",
                     xformla = ~ 1,
                     data = elections_cd_excluded,
                     est_method = "reg",
                     clustervars = "town_num"
)

es_simple_rebels <- aggte(out_rebels, type = "simple", clustervars = "town_num")

#________________________________
##gov attack
out_gov <- att_gt(yname = "government_binary",
                  gname = "first_treat",
                  idname = "town_num",
                  tname = "week",
                  xformla = ~ 1,
                  data = elections_cd_excluded,
                  est_method = "reg",
                  clustervars = "town_num"
)

es_simple_gov <- aggte(out_gov, type = "simple", clustervars = "town_num")

res_general <- c(round(es_simple_rebels$overall.att, 3), str_c("(", (round(es_simple_rebels$overall.se, 3)), ")"),
                 round(es_simple_gov$overall.att, 3), str_c("(", (round(es_simple_gov$overall.se, 3)), ")"),
                 round(es_simple_general$overall.att, 3), str_c("(", (round(es_simple_general$overall.se, 3)), ")"))

#________________________________
##table to print
to_print <- cbind(res_general, res_control, res_dur)
to_print <- rbind(to_print, c(nrow(elections_cd_excluded), nrow(elections_cd_excluded[elections_cd_excluded$to_keep == 1,]), nrow(elections_cd_excluded[elections_cd_excluded$to_keep_dur == 1,])))
print(xtable(to_print), include.rownames = F)


